import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
//0石头，1剪刀，2布
public class HW_9_caiquan {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random r=new Random();
        ArrayList<Integer> list = new ArrayList<>();
        int count=0;//总局数
        int win=0;//胜场数
        while (true){
            System.out.println("请出招(0石头，1剪刀，2布):");
            int a=sc.nextInt();
            if(a!=0&&a!=1&&a!=2){
                System.out.println("输入非法");
                continue;
            }
            int b;
            int ra=r.nextInt(2);//%50概率是否触发算法
            if(win*2<count){
                b=r.nextInt(3);//ai胜率高就纯随机
            }else if (beat1(list)){
                b=beat3(a);//如果玩家连续三次出一样的，这局必定克制前几次出招
            }else if (win*2>count&&ra==0){
                b=beat2(list);//当机器胜率低于玩家，概率触发根据玩家出最多次数来克制
            }else {b=r.nextInt(3);}
            System.out.println("ai出的是"+b);
            if(a==b){
                System.out.println("平局,再来");
            } else {
                if (wining(a,b)){
                    System.out.println("你赢了,再来");
                    win++;
                    count++;
                } else {
                    System.out.println("你输了,再来");
                    count++;
                }
            }
            if (count!=0){
                System.out.println("当前胜率:"+win+"/"+count);
            }
            list.add(a);
        }
    }
    //判断胜负,不考虑平局
    public static boolean wining(int x,int y){
        if (x==0&&y==1){
            return true;
        } else if (x==1&&y==2) {
            return true;
        } else if (x==2&&y==0) {
            return true;
        } else {return false;}
    }
    //判断玩家是否连续三次出了一样的来克制
    public static boolean beat1(ArrayList<Integer> list){
        if (list.size()>=3){
            if (list.get(list.size()-1)==list.get(list.size()-2)&&list.get(list.size()-2)==list.get(list.size()-3)){
            return true;
        }}
        return false;
    }
    //ai落后时可能触发，按人类最多出法克制
    public static int beat2(ArrayList<Integer> list){
        Random r=new Random();
        int count0=0;
        int count1=0;
        int count2=0;
        for (int i=0;i<list.size();i++){
            if (list.get(i)==0){
                count0++;
            } else if (list.get(i)==1) {
                count1++;
            } else {
                count2++;
            }
        }
        if (count0>count1&&count0>count2){
            return beat3(0);
        } else if (count1>count0&&count1>count2){
            return beat3(1);
        } else if (count2>count0&&count2>count1){
            return beat3(2);
        } else{
            return r.nextInt(3);
        }
    }
    //克制出牌
    public static int beat3(int x){
        if (x==0){
            return 2;
        }else if (x==1){
            return 0;
        } else {
            return 3;
        }
    }
}
